package homework.exp06;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;

public class D {
    static Scanner cin = null;

    static {
        try {
            cin = new Scanner(new FileInputStream(new File("src/homework/exp06/D.in")));
        } catch (IOException e) {
            cin = new Scanner(System.in);
        }
    }
    public static void main(String[] args) {

        int T = cin.nextInt();
        for (int i = 1; i <= T; i++) {
            int n = cin.nextInt();
            boolean[][] num = new boolean[27][27];
            boolean[][] flag = new boolean[27][27];
            for (int j = 0; j < n; j++) {
                String str = cin.next();
                int a = str.charAt(0) - 'A';
                int b = str.charAt(2) - 'A';
                if (str.charAt(1) == '>') {
                    num[b][a] = flag[b][a] = true;
                } else {
                    num[a][b] = flag[a][b] = true;
                }
            }

            for (int j = 0; j < num.length; j++) {
                for (int k = 0; k < num.length; k++) {
                    for (int l = 0; l < num.length; l++) {
                        if (num[j][k] && num[k][l]) {
                            num[j][l] = true;
                        }
                    }
                }
            }
            System.out.println("Case " + i + ":");
            boolean bl = false;
            for (int j = 0; j < num.length; j++) {
                for (int k = 0; k < num.length; k++) {
                    if (num[j][k] && !flag[j][k]) {
                        System.out.printf("%c<%c\n", j + 'A', k + 'A');
                        bl = true;
                    }
                }
            }
            if (!bl) {
                System.out.println("NONE");
            }
        }
    }
}